<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/11/25
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>

<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="title" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="content" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录IP" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开始时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="logintime1" id="logintime1"  autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入开始时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="logintime2" id="logintime2"  autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入结束时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit  lay-filter="doSearch">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>
</form>

<table id="newsTable" lay-filter="newsTable"></table>

<script id="toolbarNews" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="newsadd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="delBatch">批量删除</button>
    </div>
</script>

<script id="barLog" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="selRolerole">查看</a>
</script>
<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-black" style="padding: 5px">
                <input type="hidden" name="id">
                <input type="text" name="title" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入公告标题" style="height: 30px;border-radius: 10px">
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-black" style="padding: 5px">
                <textarea id="content" name="content" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>
<%--查看公告的div开始--%>
<div id="viewNewsDiv" style="padding: 10px;display: none">
    <h2 id="view_title" align="center"></h2>
    <hr>
    <div style="text-align: right">
        发布人:<span id="view_publishername"></span>
        <span style="display: inline-block;width: 20px"></span>
        发布时间:<span id="view_createtime"></span>
    </div>
    <hr>
    <div id="view_content"></div>
</div>
<%--查看公告的div结束--%>


<script src="${pageContext.request.contextPath}/static/layuicms2.0/layui/layui.js"></script>
    <script>
        var logTree;
        var newsTable;
        var miniIndex;
        var url;
        var demoTree;
        var layeditContent;
        layui.extend({
            dtree: '${pageContext.request.contextPath}/static/layuicms2.0/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
        }).use(['table','form','layer','jquery','dtree','laydate','layedit'], function(){
            var table = layui.table
            var form = layui.form
            var layer = layui.layer
            var $ = layui.jquery
            var dtree = layui.dtree
            var laydate = layui.laydate
            var layedit = layui.layedit

            //执行一个laydate实例
            laydate.render({
                elem: '#logintime1' //指定元素
                ,type :"datetime"
            });

            laydate.render({
                elem: '#logintime2' //指定元素
                ,type :"datetime"
            });

            // 初始table
            newsTable = table.render({
                elem:"#newsTable"
                ,url:"/car/news/init"
                ,page: {limit:5 ,limits:[5,10,15]} //开启分页
                ,cellMinWidth:80
                ,toolbar: '#toolbarNews' //开启头部工具栏，并为其绑定左侧模板
                ,cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'id', title: '公告ID',  fixed: 'center'}
                    ,{field: 'title', title: '公告标题',   fixed: 'center'}
                    ,{field: 'content', title: '公告内容',  fixed: 'center'}
                    ,{field: 'createtime', title: '创建时间',   fixed: 'center'}
                    ,{field: 'publishername', title: '发布人',   fixed: 'center'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barLog', width:280}
                ]]
            })

            // form表单提交  -- 查询功能
            form.on("submit(doSearch)",function(data) {
                newsTable.reload({
                    url:"/car/news/init"
                    ,where :data.field
                    ,page:{
                        curr:1
                    }
                })
                return false;
            })


            // 头部工具栏添加事件
            table.on('toolbar(newsTable)',function(obj) {
                var data = obj.data
                if(obj.event == "delBatch"){
                var checkStatus = table.checkStatus(obj.config.id)//获取选中的条数
                    if(checkStatus.data.length==0){//选择的条数=0就提示
                        layer.msg("您至少选择一条进行删除")
                    }
                    var ids=new Array() //申明数组
                    for(var i=0;i<checkStatus.data.length;i++){
                          ids.push(checkStatus.data[i].id)//把选中条数的Id循环装进ids数组
                    }
                    $.post("/car/news/deleteAll",{"ids":ids},function (r) {//传ids到contorlle层进行批量删除
                        layer.msg(r.msg)
                        newsTable.reload()
                    })
                }else if(obj.event == "newsadd"){
                    newsAdd()
                }

            })

            function newsAdd(){
                miniIndex = layer.open({
                    type:1 // 1（页面层）
                    , title:"添加公告"
                    , content:$("#saveOrUpdateDiv")
                    , area : ['700px', '500px']
                    , success:function() {
                        // 页面数据要清除
                        $("#dataFrm")[0].reset()

                        layedit.set({	//设置图片接口
                            uploadImage: {
                                url: '/car/news/upload', //接口url
                                type: 'post'
                            }
                        });

                        layeditContent = layedit.build('content'); //建立编辑器
                        url="/car/news/add"
                    }
                })
            }

            // form表达提交 -- 新增/修改
            form.on("submit(doSubmit)",function() {
                // 获取富文本上的内容
                layedit.getContent(layeditContent)
                layedit.sync(layeditContent) //  把富文本内容同步到textarea
                layer.confirm("您确定要提交该数据么",function(ind){
                    $.post(url,$("#dataFrm").serialize(),function(data) {
                        layer.msg(data.msg)
                        layer.close(miniIndex)
                        // 刷新table数据
                        newsTable.reload()
                    })
                    layer.close(ind)
                })



                return false;
            })

            //监听行工具事件
            table.on('tool(newsTable)', function(obj){
                var data = obj.data
                  if(obj.event == "del"){ // 删除
                    del(data)
                }else if(obj.event=="edit"){//修改
                      edit(data)
                  }else if(obj.event=="selRolerole"){//查看
                      selRolerole(data)
                  }
            })


            function edit(data) {
                // 弹出框
                miniIndex =  layer.open({
                    type:1 // 1（页面层）
                    , title:"修改公告"
                    , content:$("#saveOrUpdateDiv")
                    , area : ['700px', '500px']
                    , success:function(layero,index) {
                        layeditContent = layedit.build('content'); //建立编辑器
                        layedit.setContent(layeditContent,data.content)
                        form.val("dataFrm",data)
                        url="/car/news/update"  // 设置修改的地址

                    }
                })
            }

            function selRolerole(data) {
                // 弹出框
                miniIndex =  layer.open({
                    type:1 // 1（页面层）
                    , title:"查看公告"
                    , content:$("#viewNewsDiv")
                    , area : ['500px', '450px']
                    , success:function() {
                        $("#view_title").html(data.title)
                        $("#view_publishername").html(data.publishername)
                        $("#view_createtime").html(data.createtime)
                        $("#view_content").html(data.content)
                    }
                })
            }

            function del(data){
                layer.confirm("您确定要删除该条公告吗",function (ind) {
                    $.post("/car/news/delete",{"id":data.id},function (r) {
                                layer.msg(r.msg)
                        newsTable.reload()
                    })
                    layer.close(ind)
                })
            }


        });


    </script>
</body>
</html>
